package ch.gislb2;

import java.util.ArrayList;

import ch.gislb2.intents.AngleofViewIntent;
import ch.gislb2.intents.ArmlengthIntent;
import ch.gislb2.intents.CalculationExecutedIntent;
import ch.gislb2.intents.ExecuteCalculationIntent;
import ch.gislb2.intents.ParallaXisBaseIntent;
import ch.gislb2.interf.ParallaXisObservable;
import ch.gislb2.interf.ParallaXisObserver;
import ch.gislb2.interf.ParallaXisView;

public class ParallaView implements ParallaXisView, ParallaXisObservable{
	private DistanceCalculatorActivity activity;
	private double armLength;
	private float angleOfView;
	private ArrayList<ParallaXisObserver> observers;
	
	public ParallaView(DistanceCalculatorActivity act)
	{
		this.activity = act;
		this.observers = new ArrayList<ParallaXisObserver>();
	}

	public void showDiastance(double dist) {
		activity.showDistance(dist);		
	}
	public void calculateDistance()
	{
		//this.notifyObservers(new UpdateObject(UpdateObject.UpdateType.EXECUTECALCULATION,-1));
		this.notifyObservers(new ExecuteCalculationIntent(this, this.getAlpha1(), this.getAlpha2(), this.getAngleOfView(), this.getArmlength()));
	}
	public void setArmlength(double length)
	{
		this.armLength = length;	
		//this.notifyObservers(new UpdateObject(UpdateObject.UpdateType.ARMLENGTHCHANGED,length));
		this.notifyObservers(new ArmlengthIntent(this, length));
	}
	public double getArmlength()
	{
		return this.armLength;
	}
	public void setAngleOfView(float aov)
	{
		this.angleOfView = aov;
		//this.notifyObservers(new UpdateObject(UpdateObject.UpdateType.AOVCHANGED,aov));
		this.notifyObservers(new AngleofViewIntent(this, aov));
	}
	public double getAngleOfView()
	{
		return this.angleOfView;
	}
	public double getAlpha1()
	{
		return this.activity.getAlpha1();
	}
	public double getAlpha2()
	{
		return this.activity.getAlpha2();
	}

	public boolean addObserver(ParallaXisObserver observer) {
		if(observer != null && observer instanceof ParallaXisObserver)
		{
			return this.observers.add(observer);
		}
		else
			return false;
		
	}
	public boolean removeObserver(ParallaXisObserver observer) {
		if(observer != null && observer instanceof ParallaXisObserver)
		{
			return this.observers.remove(observer);
		}
		else
			return false;
		
	}

	public void notifyObservers(ParallaXisBaseIntent obj) {
		for(ParallaXisObserver ob:this.observers)
		{
			ob.update(this, obj);
		}
		
	}

	public void setIntent(ParallaXisBaseIntent intent) {
		if(intent != null)
		{
			if(intent instanceof CalculationExecutedIntent)
			{
				CalculationExecutedIntent data = (CalculationExecutedIntent)intent;
				System.out.println("View: Berechnung ist erfolgt.Gerechnete Distanz: "+data.getValue());
				this.showDiastance(data.getValue());
			}
			else
				throw new IllegalArgumentException("Unsupported Intent.");
		}
		else 
			throw new IllegalArgumentException("ParallaModel: Intent was null.");

	}

	

}
